<?php

class Aliexpress extends AbstractParser
{

//	protected $_url = 'http://www.aliexpress.com/category/63705/earphones-headphones.html?g=y'; // headphones
//	protected $_url = 'http://www.aliexpress.com/premium/category/4406.html?g=y&needQuery=n&SortType=total_weight_score_desc&isrefine=y'; // memory card
	protected $_url = 'http://www.aliexpress.com/premium/category/1204.html?minPrice=&maxPrice=50&SearchText=&shipCountry=all&isRtl=all&isOnSale=all&CatId=1204&g=y&pvId=&isPremium=y&needQuery=n'; // bike

	public function getRow($html)
	{
		$result = array();
		
		$html = $this->_cutSpaces( $html );

//        echo $html; exit;

		$pattern = '<h1.*class="product-name".*>(.*)</h1>';
		$matched = $this->_match( $pattern, $html );
//        print_r($matched); exit;
		
		if(empty($matched[1][0]))
			return $result;

        $result['title'] = $matched[1][0];

		$pattern = '<span.*id="sku-price".*>(.*)</span>';
		$matched = $this->_match( $pattern, $html );
		
//		print_r($matched); exit;

        if(empty($matched[1][0]))
            return $result;

        $result['price'] = $matched[1][0];


		$pattern = '<div.*class="ui-box.*(product-params|product-custom-desc|pnl-packaging).*".*>(.*)</div>';
		$matched = $this->_match( $pattern, $html );

//		print_r($matched); exit;

        if(!empty($matched[2][0]))
        {
            $result['description'] = '';
            $result['description'] .= $matched[2][0];
            $result['description'] .= $matched[2][1];
            $result['description'] .= $matched[2][2];
        }

        $pattern = '<div.*class="ui-image-viewer-thumb-wrap".*><img.*src="(.*)_350x350\.jpg".*></div>';
        $matched = $this->_match( $pattern, $html );

//		print_r($matched); exit;

        if(!empty($matched[1][0]))
            $result['photo'] = $matched[1][0];

        $pattern = 'imageBigViewURL=\[(.*)]';
        $matched = $this->_match( $pattern, $html );

        if(!empty($matched[1][0]))
        {
            $pattern = '"(.*)"';
            $matched = $this->_match( $pattern, $matched[1][0] );

            if(!empty($matched[1][0]))
                $result['photos'] = $matched[1];
        }

//        echo $html; exit;

        if(empty($matched[1][0]))
            return $result;

        $result['url'] = $this->getUrl();

        return $this->_addRow($result);
	}

    public function getNextPage($html)
    {
        $result = array();

        $html = $this->_cutSpaces( $html );

//        echo $html; exit;

        $pattern = '<span class="ui-pagination-active">.*</span><a rel="nofollow" href="(.*)".*>(\d*)</a>';
        $matched = $this->_match( $pattern, $html );
//        print_r($matched); exit;

        if(empty($matched[1][0]))
            return $result;

        $result = $matched[1][0];

        return $result;
    }

    public function getRowset($html)
    {
        $result = array();

        $html = $this->_cutSpaces( $html );

//        echo $html; exit;

        $pattern = '<div.*id="list-items".*>(.*)</ul></div>';
        $matched = $this->_match( $pattern, $html );
//        print_r($matched); exit;

        if(empty($matched[1][0]))
            return $result;

        $listHtml = $matched[1][0];

        //print_r($matched); exit;

        $pattern = '<a class="picRind.*" href="(.*)"><img.*src="(.*)".*></a>';
        $matched = $this->_match( $pattern, $listHtml );

//        print_r($matched); exit;

        if(empty($matched[1]))
            return $result;

        foreach($matched[1] as $i => $url)
        {
            $result[$i]['url'] = $url;

            if(isset($matched[2][$i]))
                $result[$i]['photo'] = $matched[2][$i];
        }

        
//        $pattern = '<div class="ui-pagination-navi util-left">(.*)>Next</a></div>';
        $pattern = '<span class="(ui-pagination-active|page-cur)">.*</span><a.*href="(.*)">.*';
        $matched = $this->_match( $pattern, $html );

        $aReturn = array();
        if(!empty($matched[2]) && !empty($matched[2][0]))
            $aReturn['page'] = $matched[2][0];

//        print_r($matched); exit;

        $aReturn['rowset'] = $this->_addRowset($result);

        return $aReturn;
    }

    public function getItem($html)
    {}

}